package cn.zhuama.org.controller;


import cn.zhuama.basic.annotation.PreAuthorize;
import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Department;
import cn.zhuama.org.mapper.DepartmentMapper;
import cn.zhuama.org.query.DepartmentQuery;
import cn.zhuama.org.service.IDepartmentService;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能") //：打在Controller对Controller做描述
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;
    //查一个 localhost:8080/department?id=1   findOne(@param("id")Long id)    默认，根据参数名字来决定的
    @ApiOperation(value = "通过ID查询" ) //：打在方法上，对方法做描述
    @GetMapping("/{id}")//get  http://localhost:8080/department/1
    @PreAuthorize(name = "id查找部门",value = "department/findOne") //value后面用来做权限校验
    public Department findOne(@ApiParam(name="ID属性",required = true) @PathVariable("id") Long id){
        return departmentService.queryOne(id);
    }
    //查所有
    @GetMapping
    @PreAuthorize(name = "查询所有部门",value = "department/findAll") //value后面用来做权限校验
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    @DeleteMapping("/{id}")// @return   delete http://localhost:8080/employee/1
    @ApiOperation(value = "删除接口")
    @Transactional
    @PreAuthorize(name = "删除部门",value = "department/del") //value后面用来做权限校验
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    @PreAuthorize(name = "批量删除接口",value = "department/patchDelete") //value后面用来做权限校验
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return  new AjaxResult(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    @PreAuthorize(name = "添加修改部门",value = "department/addOrUpdate") //value后面用来做权限校验
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PostMapping
    @PreAuthorize(name = "部门分页查询",value = "department/queryPage") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return  departmentService.queryPage(departmentQuery);
    }

    //DepartmentController
    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
